package com.imay.live.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.R;
import com.imay.live.helper.UrlHelper;
import com.imay.live.view.DekaronImageView;
import com.imay.live.viewtype.SupperModelType;

import java.util.List;

/**
 * Created by liangzhenxiong on 2017/4/6 0006.
 * 超胆播
 */

public class SupperTypeAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {


    public final static int VIEWTYPE_LINEAR_CONTENT = 0x113;//推荐结果

    int priv, gridpriv;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public SupperTypeAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(VIEWTYPE_LINEAR_CONTENT, R.layout.suppertype_item);

    }


    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        if (item.getItemType() == VIEWTYPE_LINEAR_CONTENT) {
            if (item instanceof SupperModelType) {
                if (priv == 0) {
                    priv = (int) helper.getConvertView().getContext().getResources().getDimension(R.dimen.dp_6);
                    gridpriv = priv * 4 / 3;
                }
                int leftMargin;
                int rightMargin;
                int postion = helper.getAdapterPosition() - getHeaderLayoutCount();
                int un = postion % 3;
                if (un == 0) {
                    leftMargin = priv;
                    rightMargin = gridpriv - priv;
                } else if (un == 1) {
                    leftMargin = priv - gridpriv + priv;
                    rightMargin = gridpriv - priv + gridpriv - priv;
                } else {
                    leftMargin = priv - gridpriv + priv - gridpriv + priv;
                    rightMargin = priv;
                }
                SupperModelType supperModelType = (SupperModelType) item;
                FrameLayout dekoron_fl = helper.getView(R.id.dekoron_fl);
                TextView tv_no = helper.getView(R.id.tv_no);
                DekaronImageView dekoron_layout = helper.getView(R.id.dekoron_iv);
                TextView tv_example = helper.getView(R.id.tv_example);
                helper.addOnClickListener(R.id.dekoron_fl);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) dekoron_fl.getLayoutParams();
                params.leftMargin = leftMargin;
                params.rightMargin = rightMargin;
                dekoron_fl.setLayoutParams(params);
                if (!TextUtils.isEmpty(supperModelType.getNumber())) {
                    tv_no.setText(supperModelType.getNumber());
                    tv_no.setVisibility(View.VISIBLE);
                } else {
                    tv_no.setVisibility(View.GONE);
                }
                if(supperModelType.getShortLiveInfoModel().isExample()){
                    tv_example.setVisibility(View.VISIBLE);
                    tv_no.setVisibility(View.GONE);
                }else{
                    tv_example.setVisibility(View.GONE);
                }
                String image;
                if (TextUtils.isEmpty(supperModelType.getShortLiveInfoModel().getFaceUrl())) {
                    image = UrlHelper.getVideoThumbUrl(supperModelType.getShortLiveInfoModel().getVideoUrl());
                } else {
                    image = supperModelType.getShortLiveInfoModel().getFaceUrl();
                    if (!image.endsWith("webp")) {
                        image = UrlHelper.getVideoThumbUrl(supperModelType.getShortLiveInfoModel().getVideoUrl());
                    }
                }
                float w = com.imay.base.util.ViewUtil.mScreemWidth;
                int h = (int) (w * 4 / 3);
                dekoron_layout.setUri(image, (int) w, h);
            }
        }
    }
}
